{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-08-31T14:54:17.320116Z",
     "start_time": "2025-08-31T14:54:15.301365Z"
    }
   },
   "source": [
    "# 1、获取大模型\n",
    "import os\n",
    "import dotenv\n",
    "from langchain_core.messages import AIMessage\n",
    "from langchain_openai import ChatOpenAI\n",
    "\n",
    "dotenv.load_dotenv()\n",
    "\n",
    "os.environ['OPENAI_API_KEY'] = os.getenv(\"OPENAI_API_KEY1\")\n",
    "os.environ['OPENAI_BASE_URL'] = os.getenv(\"OPENAI_BASE_URL\")\n",
    "\n",
    "# 创建大模型实例\n",
    "llm = ChatOpenAI(model=\"gpt-4o-mini\")"
   ],
   "outputs": [],
   "execution_count": 1
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-31T15:03:46.140391Z",
     "start_time": "2025-08-31T15:02:43.693506Z"
    }
   },
   "cell_type": "code",
   "source": [
    "from langchain_core.messages import HumanMessage\n",
    "from langchain_core.prompts import ChatPromptTemplate\n",
    "from langchain_core.messages.ai import AIMessage\n",
    "\n",
    "\n",
    "def chat_with_model(answer):\n",
    "    # 2、提供提示词模板：ChatPromptTemplate\n",
    "    prompt_template = ChatPromptTemplate.from_messages([\n",
    "        (\"system\", \"你是一个人工智能的助手\"),\n",
    "        (\"human\", \"{question}\")\n",
    "    ])\n",
    "\n",
    "    while True:\n",
    "        # 3、获取chain，并调用大模型得到响应\n",
    "        chain = prompt_template | llm\n",
    "        response = chain.invoke({\"question\": answer})\n",
    "\n",
    "        # 4、输出大模型的响应\n",
    "        print(f\"模型回复：{response.content}\")\n",
    "\n",
    "        # 5、继续获取用户的问题\n",
    "        user_input = input(\"你还有其他问题吗？(输入'退出'时，结束会话)\")\n",
    "\n",
    "        # 6、指明退出循环的方式\n",
    "        if (user_input == \"退出\"):\n",
    "            break\n",
    "\n",
    "        # 7、将上述新生成的消息存放到提示词模板的消息列表中\n",
    "        prompt_template.messages.append(AIMessage(content=response.content))\n",
    "        prompt_template.messages.append(HumanMessage(content=user_input))\n",
    "\n",
    "\n",
    "chat_with_model(\"你好，很高兴认识你！\")"
   ],
   "id": "354f5ed81ce93c61",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "模型回复：你好！很高兴认识你！有什么我可以帮助你的吗？\n",
      "模型回复：李白的名字中“白”字并不一定表示他肤色的苍白，而是与他的个性、才华及其诗歌风格相关。李白是唐代著名的诗人，以豪放不羁、才情横溢著称。他的诗作中常常表达对自由和理想的追求，而他的个性也被认为是洒脱奔放的。虽然名字中有“白”字，但实际上他的肤色并没有明确的记载。相信这更是一种文化象征，而非字面的意思。\n",
      "模型回复：李白有许多著名的诗篇，以下是一些广为人知的作品：\n",
      "\n",
      "1. **《将进酒》** - 这首诗表达了李白豪放的酒文化和对人生的感慨，开篇有“将进酒，杯莫停”。\n",
      "\n",
      "2. **《静夜思》** - 这首诗描绘了他在静夜中思念家乡的情景，尤其是“床前明月光，疑是地上霜”。\n",
      "\n",
      "3. **《庐山谣》** - 这一篇描绘了庐山的美丽景色，并表达了他对自然的热爱。\n",
      "\n",
      "4. **《早发白帝城》** - 描述了清晨从白帝城出发的壮阔景象，“朝辞白帝彩云间，千里江陵一日还”。\n",
      "\n",
      "5. **《月下独酌》** - 这首诗表达了李白在月下独自饮酒时的情感，体现了他对自由与孤独的思考。\n",
      "\n",
      "6. **《赠汪伦》** - 通过描写朋友之间的情谊，展示了李白深厚的友谊。\n",
      "\n",
      "李白的诗风独特，语言优美，充满了豪情与奔放，深受后人喜爱。\n",
      "模型回复：你问题中的诗人是李白。他是唐代著名的诗人，以其豪放不羁的个性和优美的诗作在中国文学史上占有重要地位。如果你想了解更多关于李白的作品或生平，随时可以问我！\n"
     ]
    }
   ],
   "execution_count": 3
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
